package com.project.smarthome.common.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.makeramen.roundedimageview.RoundedTransformationBuilder;
import com.project.smarthome.R;
import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Target;
import com.squareup.picasso.Transformation;

import java.io.File;

/**
 * Created by L on 2016/6/23.
 */
public final class ImageLoader {

    public static void loadRoundedCornerImage(Context context, RoundedImageView imageView, int radiusInDp, String url) {
        Transformation transformation = new RoundedTransformationBuilder()
                .cornerRadiusDp(radiusInDp)
                .oval(false)
                .build();
        Picasso.with(context)
                .load(url)
                .fit()
                .transform(transformation)
                .into(imageView);
    }

    public static void loadRoundedCornerImage(Context context, RoundedImageView imageView, int radiusInDp, int imgRes) {
        Transformation transformation = new RoundedTransformationBuilder()
                .cornerRadiusDp(radiusInDp)
                .oval(false)
                .build();
        Picasso.with(context)
                .load(imgRes)
                .fit()
                .transform(transformation)
                .into(imageView);
    }

    public static void loadImageFile(Context context, ImageView imageView, File imgFile, int placeHolderRes) {
        Transformation transformation = new RoundedTransformationBuilder()
                .oval(true)
                .build();
        Picasso.with(context)
                .load(imgFile)
                .placeholder(placeHolderRes)
                .fit()
                .transform(transformation)
                .into(imageView);
    }

    public static void loadImageUrl(Context context, ImageView imageView, String url, int placeHolderRes) {
        Transformation transformation = new RoundedTransformationBuilder()
                .build();
        Picasso.with(context)
                .load(url)
                .placeholder(placeHolderRes)
                .fit()
                .transform(transformation)
                .into(imageView);
    }

    public static void loadAvatarImage(Context context, RoundedImageView imageView, int radiusInDp, String url) {
        if (TextUtils.isEmpty(url))
            return;
        Transformation transformation = new RoundedTransformationBuilder()
                .cornerRadiusDp(radiusInDp)
                .oval(true)
                .build();
        Picasso.with(context)
                .load(url)
                .fit()
                .placeholder(R.mipmap.login_icon)
                .transform(transformation)
                .into(imageView);
    }

    public static void loadAvatarImage(Context context, RoundedImageView imageView, int radiusInDp, File imgFile) {
        Transformation transformation = new RoundedTransformationBuilder()
                .cornerRadiusDp(radiusInDp)
                .oval(true)
                .build();
        Picasso.with(context)
                .load(imgFile)
                .fit()
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .placeholder(R.mipmap.login_icon)
                .transform(transformation)
                .into(imageView);

    }

    public static void loadLocalCircleImage(Context context, RoundedImageView imageView, int radiusInDp, int iconRes) {
        Transformation transformation = new RoundedTransformationBuilder()
                .cornerRadiusDp(radiusInDp)
                .oval(true)
                .build();
        Picasso.with(context)
                .load(iconRes)
                .fit()
                .transform(transformation)
                .into(imageView);
    }

    public static void loadRoundedCornerImageCenter(Context context, final RoundedImageView imageView, int radiusInDp, String url) {
        Transformation transformation = new RoundedTransformationBuilder()
                .cornerRadiusDp(radiusInDp)
                .oval(false)
                .build();
        Picasso.with(context)
                .load(url)
                .transform(transformation)
                .into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                        int position = (int) imageView.getTag();
                        imageView.setImageBitmap(bitmap);
                    }

                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {

                    }

                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {

                    }
                });
    }

}
